package com.superimprove;

import supermaket.UserInfo;

import java.util.HashSet;
import java.util.Scanner;

public class Manager {
    public static final Scanner input = new Scanner(System.in);
    public static HashSet<supermaket.UserInfo> u = new HashSet<>();

    static {
        u.add(new supermaket.UserInfo(1001, "冯程程", "铂金卡", 2000, "合法账户"));
        u.add(new supermaket.UserInfo(1002, "王大锤", "钻石卡", 5000, "被禁账户"));
        u.add(new supermaket.UserInfo(1003, "张巧巧", "金卡", 500, "合法账户"));
    }

    public static void main(String[] args) {
        System.out.println("================欢迎来到超市管理系统================");
        System.out.println("1.查询会员信息");
        System.out.println("2.删除会员信息");
        System.out.println("3.添加会员信息");
        System.out.println("4.修改会员积分");
        System.out.println("5.退出");
        System.out.println("================================================");
        int choice = input.nextInt();
        while (true) {
            switch (choice) {
                case 1:
                    findAll();
                    break;
                case 2:

                    break;
                case 3:

                    break;
                case 4:

                    break;
                case 5:
                    System.out.println("欢迎下次光临");
                    break;
                default:
                    System.out.println("输入错误，请重新输入");
                    break;

            }
        }
    }

    //判断id是否存在
    public static boolean isExist(int id) {
        for (supermaket.UserInfo userInfo : u) {
            if (userInfo.getId() == id) {
                return true;
            }
        }
        return false;
    }

    //1.查询所有会员信息
    public static void findAll() {
        System.out.println("编号\t会员名称\t会员类型\t会员积分\t会员状态");
        for (supermaket.UserInfo userInfo : u) {
            System.out.println(userInfo.getId() + "\t" +
                    userInfo.getName() + "\t" +
                    userInfo.getType() + "\t" +
                    userInfo.getScore() + "\t" +
                    userInfo.getLegal());
        }
    }

    //2.删除会员信息
    public static void delete() {
        System.out.println("请输入您想删除的会员id");
        int id = input.nextInt();
        //判断id是否存在
        if (isExist(id)) {
            //创建索引
            supermaket.UserInfo userInfo = null;
            //遍历集合查到符合id的一项
            for (supermaket.UserInfo info : u) {
                if (info.getId() == id) {
                    userInfo = info;
                }
                if (userInfo != null) {
                    System.out.println("请确认是否删除该会员信息(y/n)");
                    String choice = input.next();
                    if (choice.equals("y")) {
                        u.remove(userInfo);
                        System.out.println("删除成功");
                        findAll();
                        return;
                    }
                }
            }
        }
        System.out.println("该会员不存在");
    }
    //3.添加会员信息
    public static void add() {
        System.out.println("请输入会员id");
        int id = input.nextInt();
        if (isExist(id)) {
            System.out.println("该会员已存在");
        } else {
            System.out.println("请输入会员名称");
            String name = input.next();
            System.out.println("请输入会员类型");
            String type = input.next();
            System.out.println("请输入会员积分");
            int score = input.nextInt();
            System.out.println("请输入会员状态");
            String legal = input.next();
            //新建一个对象存入数组
            u.add(new supermaket.UserInfo(id, name, type, score, legal));
            System.out.println("添加成功");
        }
    }

    //4.修改会员积分
    public static void update() {
        System.out.println("请输入会员id");
        int id = input.nextInt();
        if (isExist(id)) {
            System.out.println("请输入会员积分");
            int score = input.nextInt();
            for (UserInfo userInfo : u) {
                if (userInfo.getId() == id) {
                    userInfo.setScore(score);
                }
            }
            System.out.println("修改成功");
            findAll();
        }
        System.out.println("该会员不存在");

    }
    //5.退出
    public static void exit() {
        System.out.println("欢迎下次光临");
    }
}
